<?php

/**
 * This is the model class for table "friendly_link".
 *
 * The followings are the available columns in table 'friendly_link':
 * @property string $id
 * @property string $name
 * @property string $image
 * @property string $url
 * @property string $target
 * @property string $sort
 * @property integer $status
 * @property string $created_at
 * @property string $updated_at
 * @property integer $expire_at
 * @property integer $type
 * @property string $from_url
 * @property string $rel
 */
class FriendlyLink extends CActiveRecord
{
    const DISPLAY_YES = 1;
    const DISPLAY_NO = 0;
    const TYPE_DEFAULT = 0; //默认
    const TYPE_LINK = 1; //交换链接
    const TYPE_SALE = 2; //出售链接

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{friendly_link}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, url', 'required'),
			array('status, type', 'numerical', 'integerOnly'=>true),
			array('name, image, url, target, from_url, rel', 'length', 'max'=>255),
			array('sort', 'length', 'max'=>20),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, name, image, url, target, sort, status, created_at, updated_at, expire_at, type, from_url, rel', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => '网站名称',
			'image' => '图片url',
			'url' => '链接地址',
			'target' => '新窗口打开', //_blank新窗口,_self本窗口
			'sort' => '排序',
			'status' => '状态', //0禁用,1启用
			'created_at' => '创建时间',
			'updated_at' => '修改时间',
			'expire_at' => '到期时间',
			'type' => '类型', //1友情链接2交易链接
			'from_url' => '来源URL',
			'rel' => '外链方式',//external nofollow,null
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('image',$this->image,true);
		$criteria->compare('url',$this->url,true);
		$criteria->compare('target',$this->target,true);
		$criteria->compare('sort',$this->sort,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('created_at',$this->created_at,true);
		$criteria->compare('updated_at',$this->updated_at,true);
		$criteria->compare('expire_at',$this->expire_at);
		$criteria->compare('type',$this->type);
		$criteria->compare('from_url',$this->from_url,true);
		$criteria->compare('rel',$this->rel,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return FriendlyLink the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

    /**
     * 前台获取友情链接
     * @param $cid
     * @param int $limitNum
     * @return array
     */
    public static function getFriendlyLinks($limitNum=1){
        $criteria = new CDbCriteria();
        $criteria->condition = 'status = '.self::DISPLAY_YES;
        $criteria->order = 'sort ASC,id DESC';
        $criteria->limit = $limitNum;
        return self::model()->findAll($criteria);
    }

	public function afterFind()
    {
        parent::afterFind(); // TODO: Change the autogenerated stub
        if($this->expire_at){
            $this->expire_at = date('Y-m-d H:i',$this->expire_at);
        }else{
            $this->expire_at = '';
        }
        return true;
    }

    public function beforeSave()
    {
        parent::beforeSave();
        $this->expire_at = strtotime($this->expire_at);
        if($this->isNewRecord){
            $this->created_at = time();
        }else{
            $this->updated_at = time();
        }
        return true;
    }

}
